import {request} from '../../utils/http'

Page({
  data: {
    member: {
      name: '',
      mobile: '',
      token: ''
    },
    product_id: 0,
    store_id: 0,
    mobile_login:1,
    kefu_show:0
  },
  // onGetAuthorize(res) {

  //   my.getAuthCode({
  //     scopes: ['auth_base'],
  //     success: res => {
  //       console.log(res)
  //       request('/login', 'post', {code: res.authCode}).then(res => {
  //         my.setStorage({
  //           key: 'userInfo',
  //           data: {
  //             ...res.data
  //           },
  //           success: function() {
  //             console.log('缓存写入成功')
  //           }
  //         })

  //         if (!res.data.member.mobile) {

  //         }
          
  //         this.setData({
  //           user: res.data.member,
  //           canIUseAuthButton: true
  //         })

          

  //       })
  //     },
  //   });
    
  // },
  getPhoneNumber() {
    const ext = my.getExtConfigSync()
    my.getPhoneNumber({
      // protocols: {
      //   isvAppId: '2021003144609376'
      // },
      success: (res) => {
        let response = JSON.parse(res.response);
        console.log(response);
        if (response.response.code) {
          my.showToast({content: response.response.subMsg})
          return;
        }

        my.getAuthCode({
          scopes: 'auth_base',
          success: ({ authCode }) => {
            console.log({
              authCode,
              encryptedData: response.response
            })
            // return
            request('/alipay/get_mobile', 'post', {
              encryptedData: response.response,
              authCode
            }).then(res2 => {
              console.log(res2)
              this.setData({
                member: {
                  ...res2.data
                }
              })
    
              my.setStorage({
                key: 'userInfo',
                data: {
                  ...res2.data
                },
                success: function() {
                  console.log('缓存写入成功')
                }
              })
    
              if (this.data.product_id) {
                my.navigateTo({ url: '../orderConfirm/index?id=' + this.data.product_id + '&storeId=' + this.data.store_id})
              }
              
            })
          },
        })
        
      },
      fail: (res) => {
        console.log(res);
        console.log('getPhoneNumber_fail');
      },
    });
  },
  handleClose() {
    this.setData({
      isNoBtnShow: false
    })
  },
  linkOrder() {
    if (!this.data.member.mobile) {
      my.showToast({content: '请登录后再操作'});
      return;
    }
    my.navigateTo({ url: '../order/index?pay_method=0'})
  },
  bankInfo(){
    if (!this.data.member.mobile) {
      my.showToast({content: '请登录后再操作'});
      return;
    }

    my.navigateTo({url:'/pages/bankinfo/bankinfo'})
  },
  linkFullOrder(){
    if (!this.data.member.mobile) {
      my.showToast({content: '请登录后再操作'});
      return;
    }
    my.navigateTo({ url: '../full-order/full-order?pay_method=1'})
  },
  coupon(){
    if (!this.data.member.mobile) {
      my.showToast({content: '请登录后再操作'});
      return;
    }
    my.navigateTo({ url: '/pages/coupon-list/coupon-list'})
  },
  onAuthError(err) {
    console.log(err);
  },
  complaintList(){
    if (!this.data.member.mobile) {
      my.showToast({content: '请登录后再操作'});
      return;
    }
    my.navigateTo({ url: '/pages/complaint/list/list'})
  },
  logout() {
    this.setData({
      member: {
        id: '',
        name: '',
        mobile: ''
      }
    })
    my.removeStorage({key: 'userInfo'});
  },
  onLoad(query) {
    // 页面加载
    console.info(`Page onLoad with query: ${JSON.stringify(query)}`);
    let appointment_entrance = my.getStorageSync({key:'appointment_entrance'}).data
    this.setData({
      appointment_entrance

    })
    const {product_id, storeId} = query
    if (product_id) {
      my.showToast({content: '请先登录'});
      this.setData({
        product_id: product_id,
        store_id: storeId,
      })
    }
  },
  onReady() {

  },
  onShow() {
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        console.log(res)
        if (res.data) {
          this.setData({
            member: res.data,
          })
        }
      }
    });

    my.getStorage({
      key: 'kefu_show',
      success: (res) => {
        if (res.data) {
          this.setData({
            kefu_show: res.data,
          })
        }
      }
    })
    

    my.getStorage({
      key: 'mobile_login',
      success: (res) => {
        if (res.data) {
          this.setData({
            mobile_login: res.data,
          })
        }
      }
    })
  },
  reservation(){
    my.navigateTo({
      url: '/pages/reservation/records/index'
    })
  },
  goService(){
    my.navigateTo({
      url: '/pages/kefu/kefu'
    })
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  linkMemberInfo() {
    if (!this.data.member.mobile) {
      my.showToast({content: '请登录后再操作'});
      return;
    }
    my.navigateTo({ url: '../memberInfo/index'})
  },
  goLogin(){
    my.navigateTo({
      url: '/pages/login-by-phone/login-by-phone'
    })
  }
});
